import { getAllOrderList, updateCarTrajectory } from "../../../api/index";
// import { initLine } from "../../../util/carTrajectory.js";
import { getDistance } from "../Model/getDistance.js"
let getLongitude = 0;
let getLatitude = 0;
// let map = null;
export const getPosition = (trajectoryList, orderId) => {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition( // getCurrentPosition跟setInterval类似，请求多次
      (position) => {
        const setPositon = () => {
          getLongitude = position.coords.longitude; // 经度,
          getLatitude = position.coords.latitude; // 纬度
          // console.log(getLongitude, getLatitude);
        };
        if (getLongitude && getLatitude) {
          const m = parseFloat(getDistance(
            getLongitude,
            getLatitude,
            position.coords.longitude,
            position.coords.latitude
          ).distanceStr);
          console.log('当前距离', m);
          /** 位置变动1米以上才进行更新操作 */
          if (m > 0) {
            let array = [];
            array.push(position.coords.longitude, position.coords.latitude)
            updateCarTrajectory({
              position: array,
              id: orderId
            }).then(({data}) => {
            })
            getAllOrderList().then(({data}) => {
              data.data.forEach(item => {
                if(item.id === orderId) {
                  // initLine(map, JSON.parse(item.trajectory));
                }
              })
            })
          }
          console.log('距离上一次变了'+m+'米');
        }
        setPositon();
      },
      function (error) {
        var errorType = [
          "您拒绝共享位置信息",
          "获取不到位置信息",
          "获取位置信息超时"
        ];
        alert(errorType[error.code - 1]);
      }
    );
  }
}